import tkinter as tk
from tkinterdnd2 import DND_FILES, TkinterDnD
from PIL import Image, ImageTk, ImageDraw
import os


def drag_drop(event):
    # 获取拖放的文件路径
    file_path = event.data
    if file_path:
        process_image(file_path)


def process_image(file_path):
    # 打开原始图像
    original_image = Image.open(file_path)

    # 强制调整图像大小为512x512像素
    new_size = (512, 512)
    original_image = original_image.resize(new_size)

    # 获取图像的宽度和高度
    width, height = original_image.size

    # 获取用户选择的圆角尺寸
    corner_radius = int(corner_radius_var.get())

    # 创建一个新图像，与原始图像大小一致，背景为透明
    new_image = Image.new("RGBA", (width, height), (255, 255, 255, 0))

    # 画一个圆角矩形
    mask = Image.new("L", (width, height), 0)
    draw = ImageDraw.Draw(mask)
    draw.rounded_rectangle((0, 0, width, height), corner_radius, fill=255)

    # 将原始图像放在圆角矩形内
    new_image.paste(original_image, (0, 0), mask)

    # 显示新图像
    new_image.thumbnail((300, 300))  # 缩小显示图像
    photo = ImageTk.PhotoImage(new_image)
    photo.original_path = file_path  # 保存原始文件路径到PhotoImage对象的original_path属性中
    image_label.config(image=photo)
    image_label.image = photo


def update_image(corner_radius):
    current_image = image_label.image

    if current_image:
        # 获取原始图像文件路径
        original_image_path = current_image.original_path

        if original_image_path:
            # 打开原始图像
            original_image = Image.open(original_image_path)

            # 强制调整图像大小为512x512像素
            new_size = (512, 512)
            original_image = original_image.resize(new_size)

            # 获取用户选择的圆角尺寸
            corner_radius = int(corner_radius)

            # 创建一个新图像，与原始图像大小一致，背景为透明
            new_image = Image.new("RGBA", original_image.size, (255, 255, 255, 0))

            # 画一个圆角矩形
            mask = Image.new("L", original_image.size, 0)
            draw = ImageDraw.Draw(mask)
            draw.rounded_rectangle((0, 0, *original_image.size), corner_radius, fill=255)

            # 将原始图像放在圆角矩形内
            new_image.paste(original_image, (0, 0), mask)

            # 显示新图像
            new_image.thumbnail((300, 300))  # 缩小显示图像
            photo = ImageTk.PhotoImage(new_image)
            photo.original_path = original_image_path
            image_label.config(image=photo)
            image_label.image = photo


def save_image():
    current_image = image_label.image

    if current_image:
        # 获取原始图像文件路径
        original_image_path = current_image.original_path

        if original_image_path:
            # 打开原始图像
            original_image = Image.open(original_image_path)

            # 强制调整图像大小为512x512像素
            new_size = (512, 512)
            original_image = original_image.resize(new_size)

            # 获取用户选择的圆角尺寸
            corner_radius = int(corner_radius_var.get())

            # 创建一个新图像，与原始图像大小一致，背景为透明
            new_image = Image.new("RGBA", original_image.size, (255, 255, 255, 0))

            # 画一个圆角矩形
            mask = Image.new("L", original_image.size, 0)
            draw = ImageDraw.Draw(mask)
            draw.rounded_rectangle((0, 0, *original_image.size), corner_radius, fill=255)

            # 将原始图像放在圆角矩形内
            new_image.paste(original_image, (0, 0), mask)

            # 构造保存路径，与原始图像同级目录
            save_dir = os.path.dirname(original_image_path)
            save_filename = f"rounded_{os.path.basename(original_image_path)}"
            save_path = os.path.join(save_dir, save_filename)

            # 保存当前图像到同级目录
            new_image.save(save_path)


# 创建Tkinter窗口
root = TkinterDnD.Tk()
root.title("拖拽图片并裁剪圆角")

# 设置窗口初始大小为512x512
root.geometry("512x512")

# 创建一个标签来显示图像
image_label = tk.Label(root)
image_label.pack()

# 创建一个默认的黑色图像
default_image = Image.new("RGBA", (300, 300), (0, 0, 0, 255))
default_photo = ImageTk.PhotoImage(default_image)
image_label.config(image=default_photo)
image_label.image = default_photo

# 设置窗口拖拽文件的处理函数
root.drop_target_register(DND_FILES)
root.dnd_bind('<<Drop>>', drag_drop)

# 创建一个圆角尺寸选项，范围从1到200
corner_radius_var = tk.StringVar()
corner_radius_var.set("100")  # 默认圆角半径为100
corner_radius_label = tk.Label(root, text="选择圆角尺寸 (1-200):")
corner_radius_label.pack()
corner_radius_scale = tk.Scale(root, from_=1, to=200, orient="horizontal", variable=corner_radius_var, command=update_image, resolution=1, length=300)
corner_radius_scale.pack()

# 创建保存按钮
save_button = tk.Button(root, text="保存图片", command=save_image)
save_button.pack()

root.mainloop()
